package com.ray.test.mock;

import com.github.jsonzou.jmockdata.JMockData;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bo shen
 * @Description: 数据模拟配置
 * @Class: MockConfig
 * @Package com.ray.test.mock
 * @date 2019/12/12 14:52
 * @company <p>杭州传化陆鲸科技有限公司</p>
 * @updateRecord time(修改时间)  author(修改人)   desc(修改内容)
 */
public class MockConfig {
    /**默认列表返回数量**/
    private static  Integer listSize = 2;
    /***map模拟初始化***/
    private static Map<String, Class> defaultMapFields = new HashMap<>();
    /***模拟 结果***/
    private static Map<Class<?>, Object> resultMap = new HashMap<>();
    /***全局配置**/
    private static com.github.jsonzou.jmockdata.MockConfig mockConfig = new com.github.jsonzou.jmockdata.MockConfig();
    /***默认全局配置***/
    private static Map<String, Object> defaultConifg = new HashMap();
    /***默认方法配置***/
    private static Map<String, Map<String, Object>> defaultMethodConifg = new HashMap();
    /***全局参数配置**/
    private static ThreadLocal<Map<Class<?>,Object>> paramsthreadLocal = new ThreadLocal<>();
    /***全局参数配置--返回对象**/
    private static ThreadLocal<Map<Class<?>,Object>> resultthreadLocal = new ThreadLocal<>();
    /***全局方法参数配置**/
    private static ThreadLocal<Map<String, Map<String,Object>>> methodParamsthreadLocal = new ThreadLocal<>();
    /***全局方法结果配置**/
    private static ThreadLocal<Map<Class<?>, Map<String,Object>>> methodResulrthreadLocal = new ThreadLocal<>();
    /***全局方法结果设置**/
    private static ThreadLocal<Map<String, Integer>> methodListSizethreadLocal = new ThreadLocal<>();

    static {
        defaultMapFields.put("id", Long.class);
        defaultMapFields.put("isDeleted", Byte.class);
        defaultMapFields.put("is_deleted", Byte.class);
        defaultMapFields.put("status", Byte.class);
        defaultMapFields.put("index_no", Integer.class);
        defaultMapFields.put("indexNo", Integer.class);
        defaultMapFields.put("gmt_create", Date.class);
        defaultMapFields.put("gmt_modified", Date.class);
        defaultMapFields.put("create_operator_id", Long.class);
        defaultMapFields.put("update_version", Integer.class);
        methodParamsthreadLocal.set(new HashMap<>());
        methodResulrthreadLocal.set(new HashMap<>());
        paramsthreadLocal.set(new HashMap<>());
        resultthreadLocal.set(new HashMap<>());
    }

    public static Map<String, Class> getDefaultMapFields() {
        return defaultMapFields;
    }

    /**
     * 给map添加属性
     * @param key
     * @param clazz
     */
    public static void addDefaultMapFields(String key, Class clazz) {
        defaultMapFields.put(key, clazz);
    }

    public static Map<String, Object> getDefaultConifg() {
        return defaultConifg;
    }

    /**
     * 全局  -添加一个对象属性默认值   所有拥有这个属性的对象 默认用这个值填充
     * @param key
     * @param value
     */
    public static void addDefaultConifg(String key, Object value) {
        MockConfig.defaultConifg.put(key, value);
    }

    public static Map<String, Map<String, Object>> getDefaultMethodConifg() {
        return defaultMethodConifg;
    }

    /**
     * 添加一个对象属性默认值   所有拥有这个属性的对象 默认用这个值填充 会覆盖 addDefaultConifg的属性
     * @param method
     * @param key
     * @param value
     */
    public static void addDefaultMethodConifg(String method, String key, Object value) {
        Map<String, Object> methodConfig = MockConfig.defaultMethodConifg.get(method);
        if (methodConfig == null) {
            methodConfig = new HashMap<>();
        }
        methodConfig.put(key, value);
        MockConfig.defaultMethodConifg.put(method, methodConfig);
    }

    public static com.github.jsonzou.jmockdata.MockConfig getMockConfig() {
        return mockConfig;
    }

    public static Map<Class<?>, Object> getResultMap() {
        return resultMap;
    }

    /**
     * 给map对象添加一个属性
     * @param key
     * @param value
     */
    public static void addResultMap(Class<?> key, Object value) {
        MockConfig.resultMap.put(key, value);
    }

    public static ThreadLocal<Map<String,  Map<String,Object>>> getMethodParams() {
        return methodParamsthreadLocal;
    }

    /**
     * 给方法添加一个默认参数
     * @param name
     * @param field
     * @param value
     */
    public static void addMethodParams(String name, String field,Object value) {
        Map<String,  Map<String,Object>> map = methodParamsthreadLocal.get();
        if (map == null) {
            map = new HashMap<>();
        }
        Map<String,Object> valueMap = map.get(name);
        if(valueMap == null){
            valueMap = new HashMap();
        }
        valueMap.put(field,value);
        map.put(name, valueMap);
        methodParamsthreadLocal.set(map);
    }

    public static ThreadLocal<Map<Class<?>, Map<String, Object>>> getMethodResult() {
        return methodResulrthreadLocal;
    }

    /***
     * 类方法添加一个默认返回对象
     * @param clazz
     * @param name
     * @param value
     */
    public static void addMethodResult(Class<?> clazz, String name,Object value) {
        Map<Class<?>,  Map<String,Object>> map = methodResulrthreadLocal.get();
        if (map == null) {
            map = new HashMap<>();
        }
        Map<String,Object> valueMap = map.get(clazz);
        if(valueMap == null){
            valueMap = new HashMap();
        }
        valueMap.put(name,value);
        map.put(clazz, valueMap);
        methodResulrthreadLocal.set(map);
    }

    public static ThreadLocal<Map<Class<?>, Object>> getParams() {
        return paramsthreadLocal;
    }

    /***
     * 添加一个参数
     * @param clazz
     * @param value
     */
    public static void addParams(Class<?> clazz,Object value) {
        Map<Class<?>, Object> map = paramsthreadLocal.get();
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(clazz, value);
        paramsthreadLocal.set(map);
    }

    public static Object getValue(Class<?> clazz){
        return JMockData.mock(clazz,MockConfig.getMockConfig());
    }


    public static ThreadLocal<Map<Class<?>, Object>> getResult() {
        return resultthreadLocal;
    }

    /**
     * 添加一个全局的类型 返回结果
     * @param clazz
     * @param value
     */
    public static void addResult(Class<?> clazz,Object value) {
        Map<Class<?>, Object> map = resultthreadLocal.get();
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(clazz, value);
        resultthreadLocal.set(map);
    }

    /**
     * 获取列表数量
     * @param method
     * @return
     */
    public static Integer getListSize(Method method) {
        if(method != null){
            Map<String, Integer> map = methodListSizethreadLocal.get();
            if(map != null && map.containsKey(method.getName())){
                return  map.get((method.getName()));
            }
        }
        return listSize;
    }

    /**
     * 配置列表数量
     * @param listSize
     */
    public static void setListSize(Integer listSize) {
        MockConfig.listSize = listSize;
    }

    public static ThreadLocal<Map<String, Integer>> getMethodListSizethreadLocal() {
        return methodListSizethreadLocal;
    }

    /**
     * 添加方法中每个列表的返回数据量
     * @param name
     * @param size
     */
    public static void setMethodListSize(String name,Integer size) {
        Map<String, Integer> map = methodListSizethreadLocal.get();
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(name, size);
        methodListSizethreadLocal.set(map);
    }
}
